package com.tjsinfo.tjsoa.fileExpress.action;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.tjsinfo.tjsoa.addresslist.bo.IAddressListAndFilePendingBo;
import com.tjsinfo.tjsoa.addresslist.vo.AddressListAndFilePending;
import com.tjsinfo.tjsoa.common.DealString;
import com.tjsinfo.tjsoa.common.ToolKit;
import com.tjsinfo.tjsoa.fileExpress.bo.IFileExpressFileBo;
import com.tjsinfo.tjsoa.fileExpress.bo.IFileExpressGroupBo;
import com.tjsinfo.tjsoa.fileExpress.bo.IFileExpressUserSizeBo;
import com.tjsinfo.tjsoa.fileExpress.vo.FileExpressGroup;
import com.tjsinfo.tjsoa.fileExpress.vo.TjsUserEmail;
import com.tjsinfo.tjsoa.message.bo.ITjsMessageBo;
import com.tjsinfo.tjsoa.message.vo.TjsMessage;
import com.tjsinfo.tjsoa.system.bo.ISystemUserBo;
import com.tjsinfo.tjsoa.system.vo.Files;
import com.tjsinfo.tjsoa.system.vo.TSystemUser;
import com.tjsinfo.tjsoa.system.vo.User;

public class UpLoadAction extends Action {
	private IFileExpressUserSizeBo userEmailBo;
	private IFileExpressGroupBo expressGroupBo;
	private ISystemUserBo tjsUserBo;
	private IFileExpressFileBo fileExpressFileBo;
	private IAddressListAndFilePendingBo addressListAndFilePendingBo;
	/*private ITjsMessageBo messageBo;
	public void setMessageBo(ITjsMessageBo messageBo) {
		this.messageBo = messageBo;
	}*/

	public void setAddressListAndFilePendingBo(
			IAddressListAndFilePendingBo addressListAndFilePendingBo) {
		this.addressListAndFilePendingBo = addressListAndFilePendingBo;
	}

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

		String returnString = "";

		ArrayList listFileName = new ArrayList();
		ArrayList listfileUser = new ArrayList();
		ArrayList fasongUser = new ArrayList();
		
		String jibie = "";
		String beizhu = "";
		
		//提交上传附件
		String fromFilePath=ToolKit.getProperties("com/tjsinfo/tjsoa/system/properties/systemResource.properties", this).getProperty("system.uploadFile.tempFilePath");
		String toFilePath = ToolKit.getProperties("com/tjsinfo/tjsoa/fileExpress/properties/FileExpress.properties", this).getProperty("fileupload");
		fromFilePath = fromFilePath.replace("\\\\", "\\");
		toFilePath = toFilePath.replace("\\\\", "\\");
		List<Files> fileList=DealString.fileCopyTofile(request, fromFilePath, toFilePath);
		if (fileList != null) {
			String userid = request.getParameter("userid");
			jibie = request.getParameter("jibie");
			if (request.getParameter("beizhu") != null
					&& !"".equals(request.getParameter("beizhu"))) {
				beizhu = request.getParameter("beizhu");
				if (beizhu != null && !"".equals(beizhu)) {
					beizhu = beizhu.replace("\r\n","<br/>");
					beizhu = beizhu.replace("\"", "“");
				}
			}
			Integer[] inte = new Integer[userid.split(",").length];
			for (int j = 0; j < inte.length; j++) {
				if (userid.split(",")[j] != null) {
					inte[j] = Integer.valueOf(userid.split(",")[j]);
				}
			}
			List<TSystemUser> list = tjsUserBo.findInUserIds(inte);
			

			HttpSession session = request.getSession();
			User user = (User) session.getAttribute("user");
			
			boolean bl = true;
			for (Iterator iterator = list.iterator(); iterator
					.hasNext();) {
				TSystemUser user1 = (TSystemUser) iterator.next();
				
				fasongUser.add(user1.getUserName());
				listfileUser.add(user1.getUserId());
				for (Files files : fileList) {
					if (bl) {
						listFileName.add(files.getOldFileName());
					}
					FileExpressGroup expressGroup = new FileExpressGroup();
					expressGroup = expressGroupBo.findByMainGroup(
							user1.getUserId(),
							"我的接收文件夹");
					String id = UUID.randomUUID().toString().replace("-", "");
					fileExpressFileBo.insert(id,expressGroup.getId(), user.getUser_id(), files.getOldFileName(), sdf.format(new Date()), jibie, files.getFileSize(), files.getNewFileName(), beizhu);
				}
				bl = false;
			}

			for (Iterator iterator = listfileUser.iterator(); iterator
				.hasNext();) {
				Integer userId = (Integer) iterator.next();
				AddressListAndFilePending message = new AddressListAndFilePending();
				message.setId(UUID.randomUUID().toString().replace("-", ""));
				message.setSendUserId(user.getUser_id());
				message.setSendUserName(user.getUsername());
				message.setCreateTime(new Date());
				message.setTjsUserId(userId);
				
				message.setTitle("发送新文件");
				message.setType("文件速递");
				String path1 = request.getContextPath();
				String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path1+"/fileExpress/frame.jsp";
				message.setUrl(basePath);
				
				addressListAndFilePendingBo.save(message);
				
//				TjsMessage message1 = new TjsMessage();
//				message1.setRecuserid(userId);
//				message1.setValue(user.getUsername()+"向您发送文件，请登录TJSOA进行处理。");
//				tjsMessageBo.sendNewMessage(message1);
			}
			
			String username = request.getParameter("express_username");
			if (username!=null && !"".equals(username)) {
				String[] str = username.split(",");
				for (int i = 0; i < str.length; i++) {
					if(str[i]!=null && !"".equals(str[i])){
						TjsMessage message1 = new TjsMessage();
						message1.setSendusername(user.getUsername());
						message1.setRecusername(str[i]);
						message1.setValue(user.getUsername()+"向您发送文件，请登录TJSOA进行处理。");
						//TjsMessage.send(message1);
					}
				}
			}
		}
		request.setAttribute("returnString", returnString);
		request.setAttribute("listFileName", listFileName);
		request.setAttribute("fasongUser", fasongUser);
		return mapping.findForward("ok");
	}

	public IFileExpressGroupBo getExpressGroupBo() {
		return expressGroupBo;
	}

	public void setExpressGroupBo(IFileExpressGroupBo expressGroupBo) {
		this.expressGroupBo = expressGroupBo;
	}

	public void setUserEmailBo(IFileExpressUserSizeBo userEmailBo) {
		this.userEmailBo = userEmailBo;
	}

	public void setTjsUserBo(ISystemUserBo tjsUserBo) {
		this.tjsUserBo = tjsUserBo;
	}

	public void setFileExpressFileBo(IFileExpressFileBo fileExpressFileBo) {
		this.fileExpressFileBo = fileExpressFileBo;
	}
}
